package com.zdsoft.finance.meet.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zdsoft.finance.base.service.impl.BaseServiceImpl;
import com.zdsoft.finance.common.base.CustomRepository;
import com.zdsoft.finance.common.exception.BusinessException;
import com.zdsoft.finance.meet.entity.Meeting;
import com.zdsoft.finance.meet.entity.MeetingMember;
import com.zdsoft.finance.meet.entity.MeetingProject;
import com.zdsoft.finance.meet.repository.MeetingMemberRepository;
import com.zdsoft.finance.meet.repository.MeetingProjectRepository;
import com.zdsoft.finance.meet.repository.MeetingRepository;
import com.zdsoft.finance.meet.service.MeetingService;
import com.zdsoft.finance.spi.meeting.dto.MeetingProjectStatus;
import com.zdsoft.finance.spi.meeting.dto.MeetingStatus;
import com.zdsoft.framework.core.common.util.ObjectHelper;

@Service("meetingService")
public class MeetingServiceImpl extends BaseServiceImpl<Meeting, CustomRepository<Meeting, String>>
		implements MeetingService {

	@Autowired
	MeetingProjectRepository meetingProjectRepository;

	@Autowired
	MeetingRepository meetingRepository;

	@Autowired
	MeetingMemberRepository meetingMemberRepository;

	@Override
	@Transactional
	public Meeting saveMeetingAndMeetProject(Meeting meeting, List<String> meetingProjectIds,
			Integer meetingProjectStatus) throws Exception {

		if (ObjectHelper.isNotEmpty(meeting.getId())) {

			Meeting oldMeeting = this.findOne(meeting.getId());

			List<MeetingProject> meetingProjects = meetingProjectRepository.findByIdIn(meetingProjectIds);

			// 将原有数据状态修改为待上会
			List<MeetingProject> oldMeetingProject = oldMeeting.getMeetingProjects();
			List<String> oldMeetingProjectIds = new ArrayList<String>();
			if (ObjectHelper.isNotEmpty(oldMeetingProject)) {
				for (MeetingProject meetingProject : oldMeetingProject) {
					meetingProject.setMeeting(null);
					if (ObjectHelper.isNotEmpty(meeting.getState())
							&& MeetingStatus.MEET_STATE_PEND.value.equals(meeting.getState())) {
						meetingProject.setState(MeetingProjectStatus.MEET_PROJECT_STATE_TOVOTE.value);
					} else {
						meetingProject.setState(MeetingProjectStatus.MEET_PROJECT_STATE_WAIT.value);
					}
					meetingProjectRepository.updateEntity(meetingProject);
					oldMeetingProjectIds.add(meetingProject.getId());
				}
			}

			// 删除对应的会议委员记录
			List<MeetingMember> meetingMembers = meetingMemberRepository.findByMeetingProjectIds(oldMeetingProjectIds);
			if (ObjectHelper.isNotEmpty(meetingMembers)) {
				for (MeetingMember meetingMember : meetingMembers) {
					meetingMemberRepository.logicDelete(meetingMember);
				}
			}

			// 组装数据
			oldMeeting.setMeetingMinutesNo(meeting.getMeetingMinutesNo());
			oldMeeting.setMeetingDate(meeting.getMeetingDate());
			oldMeeting.setCompereId(meeting.getCompereId());
			oldMeeting.setCompereName(meeting.getCompereName());
			oldMeeting.setRecorderId(meeting.getRecorderId());
			oldMeeting.setRecorderName(meeting.getRecorderName());
			oldMeeting.setMeetingPlace(meeting.getMeetingPlace());
			oldMeeting.setMeetingIssue(meeting.getMeetingIssue());
			oldMeeting.getMeetingProjects().clear();
			oldMeeting.setMeetingProjects(meetingProjects);
			oldMeeting.setState(meeting.getState());
			oldMeeting.setInitiatorId(meeting.getInitiatorId());
			oldMeeting.setInitiatorName(meeting.getInitiatorName());
			oldMeeting.setLaunchDateTime(meeting.getLaunchDateTime());

			meeting = this.updateEntity(oldMeeting);

			for (int i = 0; i < meetingProjects.size(); i++) {
				meetingProjects.get(i).setMeeting(meeting);
				meetingProjects.get(i).setState(meetingProjectStatus);
				meetingProjectRepository.updateEntity(meetingProjects.get(i));
			}

			return meeting;
		} else {
			List<MeetingProject> meetingProjects = meetingProjectRepository.findByIdIn(meetingProjectIds);

			meeting.setMeetingProjects(meetingProjects);
			meeting = this.saveEntity(meeting);

			for (int i = 0; i < meetingProjects.size(); i++) {
				meetingProjects.get(i).setMeeting(meeting);
				meetingProjects.get(i).setState(meetingProjectStatus);
				meetingProjectRepository.updateEntity(meetingProjects.get(i));
			}
			return meeting;
		}
	}

	@Override
	@Transactional
	public void cancelMeet(String id) throws BusinessException {
		if (ObjectHelper.isEmpty(id)) {
			throw new BusinessException("10010004", "传入参数为空");
		}

		// 则将会议置为取消状态
		Meeting meeting = meetingRepository.findOne(id);
		if (ObjectHelper.isEmpty(meeting)) {
			throw new BusinessException("10010003", "传入参数有错误");
		}
		meeting.setState(MeetingStatus.MEET_STATE_FINAL_END.value);
		meetingRepository.updateEntity(meeting);

		// 将会议项目移除会议，会议项目置为待上会状态
		List<MeetingProject> meetingProjects = meetingProjectRepository.findByMeetingId(meeting.getId());
		List<String> meetingProjectIds = new ArrayList<String>();
		if (ObjectHelper.isNotEmpty(meetingProjects)) {
			for (MeetingProject meetingProject : meetingProjects) {
				meetingProject.setState(MeetingProjectStatus.MEET_PROJECT_STATE_WAIT.value);
				meetingProject.setMeeting(null);
				meetingProjectRepository.updateEntity(meetingProject);

				meetingProjectIds.add(meetingProject.getId());
			}
		}

		// 删除对应的会议委员记录
		if (ObjectHelper.isNotEmpty(meetingProjectIds) && (meetingProjectIds.size() != 0)) {
			List<MeetingMember> meetingMembers = meetingMemberRepository.findByMeetingProjectIds(meetingProjectIds);
			if (ObjectHelper.isNotEmpty(meetingMembers)) {
				for (MeetingMember meetingMember : meetingMembers) {
					meetingMemberRepository.logicDelete(meetingMember);
				}
			}
		}
	}

}
